<template>
  <view style="height: 100vh; background: #fff">
    <image class="bg" src="../../static/home/bg.png" mode="aspectFill" />
    <u-navbar title="相片模板" bgColor="rgba(0,0,0,0)" auto-back></u-navbar>
    <view class="con">
      <view class="box">
        <view class="tabBox">
          <view @click="tabClick('1')" :class="{ item: true, active: pageActive === '1' }">美化前</view>
          <view @click="tabClick('2')" :class="{ item: true, active: pageActive === '2' }">美化后</view>
        </view>
        <image v-show="pageActive === '1'" :src="info.url" style="width: 100%; height: 903rpx" mode="aspectFill" />
        <demo-block title="123" v-show="pageActive === '2'">
          <l-painter @done="done" pathType="url" ref="poster" performance css="position:relative;">
            <l-painter-image :src="info.url" css="width:93vw;height: 903rpx;position:relative;z-index:9;object-fit: cover;" />
            <l-painter-image :src="info.textPic" :css="`min-width:150px;min-height:110px;position:absolute;${info.textPicAttribute}z-index:999`" />
          </l-painter>
        </demo-block>
      </view>

      <view class="btn">
        <u-button type="primary" shape="circle" icon="download" @click="save">下载图片</u-button>
      </view>
    </view>
  </view>
</template>
<script setup lang="ts">
import { useFilesStore } from "@/stores/files";
import { BASE_UPLOADURL } from "@/utils/request";
import { ref } from "vue";
const info = useFilesStore().videoInfo;
const poster = ref();
const picture = ref("");
const pageActive = ref("2");

const done = () => {
  console.log("绘制完成:");
};
const tabClick = (index: string) => {
  pageActive.value = index;
};
const save = () => {
  uni.showLoading({
    title: "加载中",
  });
  setTimeout(() => {
    poster.value.canvasToTempFilePath({
      fileType: "jpg",
      quality: 1,
      width: 300,
      height: 500,
      success: (res: any) => {
        console.log(res.tempFilePath);
        uni.uploadFile({
          url: BASE_UPLOADURL,
          filePath: res.tempFilePath,
          name: "file",
          header: {
            Authorization: uni.getStorageSync("token"),
          },
          success: (uploadFileRes) => {
            console.log(uploadFileRes, "uploadFileRes");
            if (!uploadFileRes.data) {
              uni.hideLoading();
              uni.showToast({
                title: "海报生成失败",
                icon: "none",
              });
            }
            console.log(JSON.parse(uploadFileRes.data).data);

            picture.value = JSON.parse(uploadFileRes.data).data?.url;
            // saveImage();
            setTimeout(() => {
              shareImg();
            }, 200);
          },
        });
      },
      fail(e: any) {
        console.log("???????????", e);
      },
    });
  }, 700);
};
const shareImg = () => {
  uni.downloadFile({
    url: picture.value, //分享的图片的链接
    success: (res) => {
      console.log(res);
      uni.showShareImageMenu({
        path: res.tempFilePath,
        complete() {
          setTimeout(() => {
            uni.hideLoading();
          }, 300);
        },
      });
    },
    complete() {
      //隐藏loading
      uni.hideLoading();
    },
    fail(err) {
      console.log(err, "shareImg");
    },
  });
};
</script>
<style lang="less" scoped>
.bg {
  width: 100%;
  height: 1362rpx;
  position: absolute;
  left: 0;
  z-index: 1;
}
.con {
  position: relative;
  padding: 0 30rpx;
  z-index: 2;
  padding-top: 190rpx;
  .box {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    position: relative;
    overflow: hidden;
    .tabBox {
      width: 250rpx;
      display: flex;
      justify-content: space-between;
      background: rgba(188, 188, 188, 0.2);
      border-radius: 34rpx 34rpx 34rpx 34rpx;
      padding: 8rpx 18rpx;
      margin-bottom: 30rpx;
      .item {
        font-size: 28rpx;
        color: #333333;
        padding: 10rpx 20rpx;
      }
      .active {
        background: #ffffff;
        border-radius: 34rpx 34rpx 34rpx 34rpx;
        color: #3b70fd;
        transition: all 0.3s ease;
      }
    }
    .qh {
      position: absolute;
      right: 32rpx;
      bottom: 32rpx;
      width: 90rpx;
      height: 90rpx;
      z-index: 99999;
    }
  }
  .btn {
    box-shadow: 0rpx 20rpx 30rpx 0rpx rgba(100, 73, 218, 0.3);
    width: 400rpx;
    margin: 90rpx auto;
    border-radius: 60rpx 60rpx 60rpx 60rpx;
  }
  .sub {
    font-size: 28rpx;
    color: #3d3d3d;
    margin-top: 28rpx;
    text {
      background: rgba(59, 112, 253, 0.17);
      border-radius: 8rpx 8rpx 8rpx 8rpx;
      font-size: 26rpx;
      color: #ffffff;
      padding: 0 10rpx;
    }
  }
}
.textB1 {
  text-align: center;
  .textImg {
    width: 100%;
    height: 360rpx;
  }
  .textBox {
    margin-top: 28rpx;
    text-align: left;
    padding: 38rpx 22rpx;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 32rpx 32rpx 32rpx 32rpx;
    border: 2rpx dashed #767676;
    font-size: 32rpx;
    color: #3d3d3d;
  }
}
</style>
